<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  :immediate-check="false"
>
    <ArticleItem
      v-for="obj in searchArr"
      :key="obj.art_id"
      :obj="obj"
      :isShow="false"
      @click="moreFn(obj.art_id)"
    ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/views/Home/components/ArticleItem'
import { searchAPI } from '@/api'
export default {
  components: {
    ArticleItem
  },
  data () {
    return {
      searchArr: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  created () {
    this.getSearchList()
  },
  name: 'SearchResult',
  methods: {
    moreFn (id) {
      // 获取文章详情`
      this.$router.push({
        path: '/article_detail',
        query: {
          aId: id
        }
      })
    },
    onLoad () {
      this.page++
      this.getSearchList()
    },
    async getSearchList () {
      const str = this.$route.params.keyword
      const res = await searchAPI({
        searchStr: str,
        page: this.page
      })
      console.log(res)
      this.searchArr = [...this.searchArr, ...res.data.data.results]
      this.loading = false
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
